<template>
  <div>
    <el-dialog
      :title="info.title + '角色'"
      :visible.sync="info.isShow"
      width="50%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="rolename">
          <el-input v-model="ruleForm.rolename"></el-input>
        </el-form-item>
        <el-form-item label="权限" prop="menus">
          <!-- data树形控件的数据 -->
          <!-- show-checkbox：显示选框 -->
          <!-- default-expand-all：默认展开所有项 -->
          <!-- node-key：选择节点的依据 -->
          <!-- highlight-current：高亮显示当前项 -->
          <!-- props：树形结构的选项 -->
          <el-tree
            :data="menuList"
            show-checkbox
            default-expand-all
            node-key="id"
            ref="tree"
            highlight-current
            :props="defaultProps"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button
            class="btn"
            v-if="info.title === '添加'"
            type="primary"
            @click="submitForm('ruleForm')"
            >立即添加</el-button
          >
          <el-button
            class="btn"
            v-if="info.title === '修改'"
            type="primary"
            @click="submitForm('ruleForm')"
            >立即修改</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addRoleFn, getMenuTreeListFn, editRoleFn } from "../../apis";
export default {
  props: ["info"],
  data() {
    return {
      ruleForm: {
        rolename: "",
        menus: [],
        status: 1,
      },
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },
      // 树形结构的数组
      menuList: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    };
  },
  created() {
    // 发请求，获取菜单
    this.getMenuList();
  },
  methods: {
    setValue(data) {
      // 将字符串转换为数组，方便勾选
      let arr = data.menus.split(',');
      this.$nextTick(()=>{
        // vue中更新DOM是异步的，所以等弹窗中的tree加载完成后，在执行setCheckedKeys这个方法
        this.$refs.tree.setCheckedKeys(arr);
      })
      // 设置弹窗中表单的值
      // 深拷贝，避免数据影响
      this.ruleForm = { ...data };
    },
    async getMenuList() {
      let res = await getMenuTreeListFn();
      // 给下拉框的数组赋值
      this.menuList = res.list;
    },
    submitForm(formName) {
      // this.$refs[formName]获取表单的dom节点
      // validate 校验函数，element 自带的函数
      this.$refs[formName].validate((valid) => {
        // valid 校验的结果
        if (valid) {
          // 获取用户勾选的选项，组成一个数组
          // getCheckedKeys是element自带的函数
          let arr = this.$refs.tree.getCheckedKeys();
          // 将数据赋值给ruleForm，准备传给后台
          this.ruleForm.menus = arr.join(',')
          this.add();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 真的添加了
    async add() {
      let res;
      if (this.info.title === "添加") {
        res = await addRoleFn(this.ruleForm);
      } else {
        res = await editRoleFn(this.ruleForm);
      }
      if (res.code === 200) {
        this.closed();
        // 通知list组件更新数据
        this.$emit("update");
        // 提示成功
        this.$message({
          type: "success",
          message: res.msg,
        });
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 关闭弹窗
    closed() {
      this.info.isShow = false;
    },
  },
};
</script>

<style>
</style>